#!/bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2024 The FreeBSD Foundation
#
# This software was developed by Cybermancer Infosec <bofh@FreeBSD.org>
# under sponsorship from the FreeBSD Foundation.
#
# PROVIDE: freebsdci
# REQUIRE: LOGIN FILESYSTEMS
# KEYWORD: firstboot

# This script is used to run the firstboot CI tests on the first boot of a
# FreeBSD image. It is automatically disabled after the first boot.
#
# The script will run the firstboot CI tests and then shut down the system.
# The tests are run in the foreground so that the system can shut down
# immediately after the tests are finished.
#
# Default test types are full and smoke.  To run only the smoke tests, set
# freebsdci_type="smoke" in /etc/rc.conf.local or /etc/rc.conf.
# To run only the full tests, set freebsdci_type="full" in
# /etc/rc.conf.local or /etc/rc.conf.

. /etc/rc.subr

name="freebsdci"
desc="Run FreeBSD CI"
rcvar=freebsdci_enable
start_cmd="firstboot_ci_run"
stop_cmd=":"
os_arch=$(uname -p)
parallelism=$(nproc)
tardev=/dev/vtbd1
metadir=/meta
istar=$(file -s ${tardev} | grep "POSIX tar archive" | wc -l)

load_rc_config $name
: ${freebsdci_enable:="NO"}
: ${freebsdci_type:="full"}
: ${freebsdci_test_filters:=""}
PATH="${PATH}:/usr/local/sbin:/usr/local/bin"

auto_shutdown()
{
	# NOTE: Currently RISC-V kernels lack the ability to
	#      make qemu exit on shutdown. Reboot instead;
	#      it makes qemu exit too.
	case "$os_arch" in
		riscv64)
			shutdown -r now
		;;
		*)
			shutdown -p now
		;;
	esac
}

smoke_tests()
{
	echo
	echo "--------------------------------------------------------------"
	echo "BOOT sequence COMPLETED"
	echo "INITIATING system SHUTDOWN"
	echo "--------------------------------------------------------------"
}

full_tests()
{
	echo
	echo "--------------------------------------------------------------"
	echo "BOOT sequence COMPLETED"
	echo "TEST sequence STARTED"
	if [ "${istar}" -eq 1 ]; then
		rm -fr ${metadir}
		mkdir -p ${metadir}
		tar xvf ${tardev} -C ${metadir}
		cd /usr/tests
		set +e
		kyua \
			-v parallelism=${parallelism} \
			test ${freebsdci_test_filters}
		rc=$?
		set -e
		if [ ${rc} -ne 0 ] && [ ${rc} -ne 1 ]; then
			exit ${rc}
		fi
		kyua report --verbose --results-filter passed,skipped,xfail,broken,failed --output test-report.txt
		kyua report-junit --output=test-report.xml
		mv test-report.* /${metadir}
		tar cvf ${tardev} -C ${metadir} .
	else
		echo "ERROR: no device with POSIX tar archive format found."
		# Don't shutdown because this is not run in unattended mode
		exit 1
	fi
	echo "TEST sequence COMPLETED"
	echo "INITIATING system SHUTDOWN"
	echo "--------------------------------------------------------------"
}

firstboot_ci_run()
{
	if [ "$freebsdci_type" = "smoke" ]; then
		smoke_tests
	elif [ "$freebsdci_type" = "full" ]; then
		full_tests
	fi
	auto_shutdown
}

run_rc_command "$1"
